<template>
    <div class="goods-container">

        <!--商品列表区域-->
        <div class="mui-card" v-for="(item, i) in goodslist" :key="item.id">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <mt-switch v-model="$store.getters.getGoodsSelected[item.id]"
                    @change="selectChanged(item.id, $store.getters.getGoodsSelected[item.id])"></mt-switch>
                    <img :src="item.thumb_path" alt="">
                    <div class="info">
                        <h1>{{ item.title }}</h1>
                        <span class="price">￥{{ item.sell_price }}</span>
                        <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                        <a href="" @click.prevent="remove(item.id, i)">删除</a>
                    </div>
                </div>
            </div>
        </div>

        <!--结算区域-->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner yejiao">
                    <div class="left">
                        <p>总计（不含运费）</p>
                        <p>
                            已勾选商品&nbsp;<span class="red">{{ $store.getters.getGoodsCountAndAmount['count'] }}</span>&nbsp;件
                            &nbsp;&nbsp;&nbsp;&nbsp;
                            总价&nbsp;&nbsp;<span class="red">￥{{ $store.getters.getGoodsCountAndAmount['amount'] }}</span>
                        </p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>
                </div>
            </div>
        </div>
        <p>{{ $store.getters.getGoodsSelected }}}</p>
    </div>
</template>

<script>
    //导入数字框子组件
    import numbox from '../subcomponents/shopcar_numbox.vue'
    import {Toast} from 'mint-ui'

    export default {
        name: "",
        data() {
            return {
                goodslist: []   //购物车所有商品的数据
            }
        },
        created() {   //数据加载完毕 自动调用该函数
            this.getGoodsList()
        },
        methods: {
            getGoodsList() {
                // 1. 获取到 store 中所有的商品的Id，然后拼接出一个 用逗号分隔的 字符串
                var idArr = []
                this.$store.state.car.forEach(item => idArr.push(item.id))
                this.$http.get('api/goods/getshopcarlist/' + idArr).then(result => {
                    if (result.body.status === 0) {
                        this.goodslist = result.body.message
                    } else {
                        Toast('数据加载失败。。。')
                    }
                })
            },
            remove(id, index) {
                // 点击删除，把商品从 store 中根据 传递的 Id 删除，
                // 同时，把 当前组件中的 goodslist 中，对应要删除的那个商品，使用 index 来删除
                this.goodslist.splice(index, 1)
                this.$store.commit('removeFormCar', id)
            },
            selectChanged(id, val) {
                // 每当点击开关，把最新的 快关状态，同步到 store 中
                //  console.log(id + " --- " + val);
                this.$store.commit('updateGoodsSelected', {id, selected: val})
            }
        },
        components: {
            numbox   //注册子组件
        }
    }
</script>

<style scoped lang="scss" type="text/scss">
    .goods-container {
        background-color: #eeeeee;
        overflow: hidden;

        .mui-card-content-inner {
            display: flex;
            align-items: center;
            justify-content: space-around;

            img {
                width: 60px;
            }

            .info {
                font-size: 14px;

                h1 {
                    font-size: 16px;
                }

                .price {
                    color: red;
                    font-weight: bold;
                }
            }
        }

        .yejiao {
            display: flex;
            justify-content: space-between;
            align-items: center;

            .red {
                color: red;
                font-size: 16px;
                font-weight: bold;
            }
        }
    }

</style>